﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cricket.LinqData;
using Cricket.LinqModel;


public partial class BookieRegistration : System.Web.UI.Page
{

    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    Admin admin;
    AdminCredential adminCredentail;
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //if (Session["AdminUser"] != null)
            //    SessionManager.CurrentUser = (LoggedInUser)Session["AdminUser"];
            HttpCookie cookie = Request.Cookies["Admin"];
            if (cookie == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {

                if (Convert.ToInt32(cookie["UserType"]) == Convert.ToInt32(Enumeration.UserType.SuperAdmin))
                {
                    if (Request.QueryString["Id"] != null)
                    {
                        txtUsernameBallToBall.Enabled = false;
                        txtUsernameSession.Enabled = false;
                        txtUsernameScore.Enabled = false;
                        fillAdmin(Request.QueryString["Id"].ToString());
                    }
                }
                else
                {
                    Response.Redirect("Logout.aspx");
                }
            }
        }
    }

    private void fillAdmin(string adminId)
    {
        admin  = new Admin(_dataObject);
        List<AdminResult> adminResult = new List<AdminResult>();
        admin.Id = Convert.ToInt32(adminId);
        adminResult= admin.GetAdminDetailsById(admin.Id);
        if (adminResult.Count > 0)
        {
            txtFirstName.Text = adminResult[0].FirstName;
            txtLastName.Text = adminResult[0].LastName;
            txtPhone.Text = adminResult[0].Phone;
            txtEmailAddress.Text = adminResult[0].Email;
            txtCity.Text = adminResult[0].City;
            txtState.Text = adminResult[0].State;
            txtPincode.Text = adminResult[0].Zip;
            if (adminResult[0].IsActive == true)
            {
                rdoActive.SelectedValue = "1";
            }
            else
            {
                rdoActive.SelectedValue = "0";
            }

            txtUsernameBallToBall.Text = adminResult[0].Username;
            txtBallToBallPassword.Text = adminResult[0].Password1;
            txtBallToBallPassword.Attributes.Add("value", adminResult[0].Password1);
            txtBallToBallPassword2.Text = adminResult[0].Password2;
            txtBallToBallPassword2.Attributes.Add("value", adminResult[0].Password2);

            txtUsernameScore.Text = adminResult[1].Username;
            txtPasswordScore.Text = adminResult[1].Password1;
            txtPasswordScore.Attributes.Add("value", adminResult[1].Password1);
            txtPasswordScore2.Text = adminResult[1].Password2;
            txtPasswordScore2.Attributes.Add("value", adminResult[1].Password2);

            txtUsernameSession.Text = adminResult[2].Username;
            txtPasswordSession.Text = adminResult[2].Password1;
            txtPasswordSession.Attributes.Add("value", adminResult[2].Password1);
            txtPasswordSession2.Text = adminResult[2].Password2;
            txtPasswordSession2.Attributes.Add("value", adminResult[2].Password2);            
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        admin = new Admin(_dataObject);
        admin.FirstName = txtFirstName.Text;
        admin.LastName = txtLastName.Text;
        admin.Phone = txtPhone.Text;
        admin.Email = txtEmailAddress.Text;
        admin.City = txtCity.Text;
        admin.State = txtState.Text;
        admin.Zip = txtPincode.Text;        
        //admin.IsActive = true;
        //admin.IsDeleted = false;
        if (rdoActive.SelectedValue == "1")
        {
            admin.IsActive = true;
            admin.IsDeleted = false;
        }
        else
        {
            admin.IsActive = false;
            admin.IsDeleted = true;
        }
        admin.UserType = Convert.ToInt32(Enumeration.UserType.Admin);
        if (Request.QueryString["Id"] != null)
        {
            admin.DateUpdated = System.DateTime.UtcNow;
            admin.Id = Convert.ToInt32(Request.QueryString["Id"]);
            admin.UpdateAdmin();
            adminCredentail = new AdminCredential(_dataObject);
            adminCredentail.AdminId = Convert.ToInt32(Request.QueryString["Id"]);
            adminCredentail.DeleteAdminCredentail();
            SaveAdminCredential();
            clearControl();
            Response.Redirect("ViewBookie.aspx");
            //layerSuccess.InnerHtml = "<strong>Success!</strong> Admin updated successfully.";
            //layerSuccess.Attributes.Add("style", "display:block;");
        }
        else
        {
            admin.DateAdded = System.DateTime.UtcNow;
            admin.IsFirstLogin = true;
            admin.CommonRate = 1;
            admin.Create();
            SaveAdminCredential();
            clearControl();
            layerSuccess.InnerHtml = "<strong>Success!</strong> Admin added successfully.";
            layerSuccess.Attributes.Add("style", "display:block;");
        }
    }

    private void clearControl()
    {
        txtFirstName.Text = string.Empty;
        txtLastName.Text = string.Empty;
        txtPhone.Text = string.Empty;
        txtEmailAddress.Text = string.Empty;
        txtCity.Text = string.Empty;
        txtState.Text = string.Empty;
        txtPincode.Text = string.Empty;
        txtUsernameBallToBall.Text = string.Empty;
        txtBallToBallPassword.Text = string.Empty;
        txtBallToBallPassword2.Text = string.Empty;
        txtUsernameSession.Text = string.Empty;
        txtPasswordSession.Text = string.Empty;
        txtPasswordSession2.Text = string.Empty;
        txtUsernameScore.Text = string.Empty;
        txtPasswordScore.Text = string.Empty;
        txtPasswordScore2.Text = string.Empty;
    }

    private void SaveAdminCredential()
    {
        adminCredentail = new AdminCredential(_dataObject);
        adminCredentail.AdminId = admin.Id;
        adminCredentail.BatId = 1; // Ball to Ball
        adminCredentail.Username = txtUsernameBallToBall.Text;
        adminCredentail.Password1 = txtBallToBallPassword.Text;
        adminCredentail.Password2 = txtBallToBallPassword2.Text;
        adminCredentail.DateAdded = System.DateTime.UtcNow;
        adminCredentail.Create();

        adminCredentail = new AdminCredential(_dataObject);
        adminCredentail.AdminId = admin.Id;
        adminCredentail.BatId = 2; // Session
        adminCredentail.Username = txtUsernameSession.Text;
        adminCredentail.Password1 = txtPasswordSession.Text;
        adminCredentail.Password2 = txtPasswordSession2.Text;
        adminCredentail.DateAdded = System.DateTime.UtcNow;
        adminCredentail.Create();

        adminCredentail = new AdminCredential(_dataObject);
        adminCredentail.AdminId = admin.Id;
        adminCredentail.BatId = 3; // Scorel
        adminCredentail.Username = txtUsernameScore.Text;
        adminCredentail.Password1 = txtPasswordScore.Text;
        adminCredentail.Password2 = txtPasswordScore2.Text;
        adminCredentail.DateAdded = System.DateTime.UtcNow;
        adminCredentail.Create();
    }
    protected void btnView_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewBookie.aspx");
    }
}